﻿var x = 100, y = 100;
BiolabGame = Eg.Game.extend({
  clearColor: '#0d0c0b',
  gravity: 240,
  player: null,
  mode: 0,
  lastCheckpoint: null,
  playerSpawnPos: {
    x: 0,
    y: 0
  },
  deathCount: 0,
  tubeCount: 0,
  tubeTotal: 0,
  levelTime: null,
  levelTimeText: '0',
  musicBackground: new Eg.Sound('Resource/background.mp3', false),
  camera: null,
  lastTick: 0.016,
  realTime: 0,
  showFPS: true,
  init: function () {
    var as = new Eg.AnimationSheet('Resource/biolab.png', 8, 8);
    this.backgroundAnims = {
      'Resource/biolab.png': {
        80: new Eg.Animation(as, 0.13, [80, 81, 82, 83, 84, 85, 86, 87]),
        81: new Eg.Animation(as, 0.17, [84, 83, 82, 81, 80, 87, 86, 85]),
        88: new Eg.Animation(as, 0.23, [88, 89, 90, 91, 92, 93, 94, 95]),
        89: new Eg.Animation(as, 0.19, [95, 94, 93, 92, 91, 90, 89, 88])
      }
    };
    this.camera = new Eg.Camera(Eg.sys.width / 4, Eg.sys.height / 3, 5);
    this.camera.trap.size.x = Eg.sys.width / 10;
    this.camera.trap.size.y = Eg.sys.height / 3;
    //this.camera.lookAhead.x = Eg.ua.mobile ? Eg.sys.width / 6 : 0;
    this.camera.lookAhead.x = 0;
    Eg.music.volume = 0.9;
    Eg.music.add(this.musicBackground);
    Eg.music.play();
    this.loadLevel(LevelBiolab1);
  },
  loadLevel: function (level) {
    //    if (Eg.ua.iPhone4 || Eg.ua.android) {
    //      for (var i = 0; i < level.layer.length; i++) {
    //        if (level.layer[i].name == 'background') {
    //          level.layer.erase(level.layer[i]);
    //        }
    //      }
    //    }
    this.parent(level);
    this.player = this.getEntitiesByType(EntityPlayer)[0];
    this.lastCheckpoint = null;
    this.playerSpawnPos = {
      x: this.player.pos.x,
      y: this.player.pos.y
    };
    this.deathCount = 0;
    this.tubeCount = 0;
    //this.tubeTotal = this.getEntitiesByType(EntityTestTube).length;
    this.levelTime = new Eg.Timer();
    //this.mode = BiolabGame.MODE.GAME;
    this.camera.max.x = this.collisionMap.width * this.collisionMap.tilesize - Eg.sys.width;
    this.camera.max.y = this.collisionMap.height * this.collisionMap.tilesize - Eg.sys.height;
    this.camera.set(this.player);
    //    if (Eg.ua.mobile) {
    //      for (var i = 0; i < this.backgroundMaps.length; i++) {
    //        this.backgroundMaps[i].preRender = true;
    //      }
    //    }
  },
  respawnPlayerAtLastCheckpoint: function (x, y) {
    var pos = this.playerSpawnPos;
    if (this.lastCheckpoint) {
      pos = this.lastCheckpoint.getSpawnPos()
      this.lastCheckpoint.currentAnim = this.lastCheckpoint.anims.respawn.rewind();
    }
    this.player = this.spawnEntity('player', EntityPlayer, pos.x, pos.y);
    this.player.currentAnim = this.player.anims.spawn;
    this.deathCount++;
  },
  update: function () {
//    if (Eg.input.state('cameraRight')) {
//      Eg.game.screen.x += 1;
//    }
//    if (Eg.input.state('cameraLeft')) {
//      Eg.game.screen.x -= 1;
//    }
//    if (Eg.input.state('cameraUp')) {
//      Eg.game.screen.y -= 1;
//    }
//    if (Eg.input.state('cameraDown')) {
//      Eg.game.screen.y += 1;
//    }
    this.camera.follow(this.player);
    this.parent();
  },
  draw: function () {
    this.parent();
    this.camera.draw();
        if (this.showFPS) {
          Eg.font.draw('FPS: ' + (1 / this.lastTick).round(), 4, 4);
        }
  }
});